package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.global.ConstAnalyseType;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 陆域范围分析处理器
 *
 * @author zhu jiacheng
 * @date 2020/8/4 14:57
 */
@Service(value = ConstAnalyseType.CONST_YZFXLT + "Ver1.0")
@Slf4j
public class YzfxltHandler implements IResultHandler {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理陆域范围分析返回数据,状态码:{}", httpResult.getCode());
        if (HttpStatus.SC_OK == httpResult.getCode()) {
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body:" + httpResult.getBody());

            if (HttpStatus.SC_OK == alyResult.getStatus()) {
                result.setData(httpResult.getBody());
            } else {
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
